<template>
	<view class="company">
		<uni-forms ref="form" :modelValue="formData" :rules="rules" label-width="80px">
			<uni-forms-item label="名称" required name="Title">
				<uni-easyinput v-model="formData.Title" placeholder="请输入公司/店铺名称" />
			</uni-forms-item>
			<uni-forms-item label="电话" required name="Phone">
				<uni-easyinput type="number" v-model="formData.Phone" placeholder="请输入联系电话" />
			</uni-forms-item>
			<uni-forms-item label="分享标题">
				<uni-easyinput v-model="formData.ReceiptTitle" placeholder="请输入分享标题" />
			</uni-forms-item>
			<uni-forms-item label="账期一">
				<uni-easyinput v-model="formData.AccountingperiodDays" placeholder="请输入账期一"
					@input="AccountingperiodDaysInput" />
			</uni-forms-item>
			<uni-forms-item label="账期二">
				<uni-easyinput v-model="formData.Accountingperiod2Days" placeholder="请输入账期二"
					@input="Accountingperiod2DaysInput" />
			</uni-forms-item>
			<uni-forms-item label="账期三">
				<uni-easyinput v-model="formData.Accountingperiod3Days" placeholder="请输入账期三"
					@input="Accountingperiod3DaysInput" />
			</uni-forms-item>
			<uni-forms-item label="选择行业">
				<uni-data-select v-model="formData.value" :localdata="range" @change="changeRange"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="地址">
				<uni-easyinput type="textarea" v-model="formData.Address" placeholder="请输入店铺地址" />
			</uni-forms-item>


		</uni-forms>
		<view class="fixView"> </view>
		<view class="fixBtn">
			<button :disabled="disabled" @click="submit" type="primary">完成</button>
		</view>
		<!-- <button type="primary" @click="submit">保存</button> -->
	</view>
</template>

<script>
	import {
		regPhone
	} from '../../utils/regExp.js'
	export default {
		data() {
			return {
				disabled:false,
				range: [],
				// 公司id
				CompanyId: uni.getStorageSync('CompanyInfo').Id,
				formData: {},
				rules: {
					Title: {
						rules: [{
							require: true,
							errorMessage: '请填写公司简称',
						}]
					},
					Phone: {
						rules: [{
							required: true,
							errorMessage: '请填写手机号',
						}, {
							validateFunction: (rule, value, data, callback) => {
								if (!regPhone.test(value)) return callback('手机号格式错误')
								return true
							}
						}]
					},
				},
			}
		},
		onLoad(query) {
			if (query.Id) {
				this.getCompanyInfo(query.Id)
			} else {
				this.formData = {
					SignType: "3", // 验证方式
					IscheckPhone: false, // 签收验证
					IsEvidence: false, // 存证存储
					IsPosition: "0", // 位置验证
					Title: '',
					Phone: ''
				};
			}


			// 获取行业列表数据
			this.getInDustryGetDataList()

		},
		onShow() {

		},
		methods: {
			AccountingperiodDaysInput(e) {
				this.formData.AccountingperiodDays = Number(e)
			},
			Accountingperiod2DaysInput(e) {
				this.formData.Accountingperiod2Days = Number(e)
			},
			Accountingperiod3DaysInput(e) {
				this.formData.Accountingperiod3Days = Number(e)
			},
			// 获取添加好的行业标签数据
			getInventListData() {
				console.log('打印打印打印打印')
				this.$http("post", "/BO/InDustry/GetHomeDataList", {
						Search: {
							CommpayId: this.CompanyId
						},
					})
					.then((res) => {
						if (res.Success && res.Data) {
							this.$set(this.formData, 'value', 'hangy')
							console.log('获取已经选中号的行业', res.Data, this.formData);
							this.formData.value = res.Data[0].Id
						} else {
							// this.range = []
						}
					});
			},
			// 选择行业
			changeRange(e) {
				console.log('选择行业', e, this.formData)

			},
			// 获取行业列表数据
			getInDustryGetDataList() {
				this.$http('post', '/BO/InDustry/GetDataList', {
					CompanyId: this.CompanyId
				}).then(res => {
					if (res.Success) {
						res.Data.forEach((item, index) => {
							let obj = {
								value: item.Id,
								text: item.industryName
							}
							// arr.push(item.industryName)
							console.log('item', item.industryName)
							this.range.push(obj)

						})
					}

				})
			},
			getCompanyInfo(Id) {
				this.$http('post', '/Base_Manage/Base_Company/GetTheData?id=' + Id, {}).then(res => {
					if (res.Success) {
						this.formData = res.Data
						console.log(this.formData);
						// 获取添加好的行业标签数据
						this.getInventListData()
					} else uni.showToast({
						icon: 'none',
						title: res.Msg
					})
				})
			},
			// 完成
			submit() {
				// let arr = this.range.filter(item => item.isChecked).map(item => {
				// 	return {
				// 		CompanyId: this.CompanyId,
				// 		industryId: item.Id
				// 	}
				// })
				this.$refs['form'].validate().then(res => {
					uni.showLoading()
					if (!this.formData.Id) this.formData.TemId = '1646717019292176384'
           
					 this.disabled = true
					// 更新店铺缓存账期信息
					let shopInfo = uni.getStorageSync('shopInfo')
					let newAccountingperiodDays = Number(this.formData.AccountingperiodDays)
					let newAccountingperiod2Days = Number(this.formData.Accountingperiod2Days)
					let newAccountingperiod3Days = Number(this.formData.Accountingperiod3Days)
					this.formData.AccountingperiodDays = newAccountingperiodDays
					this.formData.Accountingperiod2Days = newAccountingperiod2Days
					this.formData.Accountingperiod3Days = newAccountingperiod3Days

					// 重新存储店铺信息里的账期信息
					uni.setStorageSync('shopInfo', shopInfo)
					this.$http('post', '/Base_Manage/Base_Company/SaveData', this.formData).then(res => {
						uni.hideLoading()
						if (res.Success) {
							uni.showToast({
								icon: 'none',
								title: '保存成功'
							})


							// 如果有选择行业  调用行业保存接口
							if (this.formData.value) {
								let obj = {
									CompanyId: this.CompanyId,
									industryId: this.formData.value
								}
								this.getAddIndustryCompany(obj)
							}
							// 返回上一页
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						} else uni.showToast({
							icon: 'none',
							title: res.Msg
						})
					})
				}).catch(err => {
					console.log(err);
				})
			},
			// 保存行业
			getAddIndustryCompany(arr) {
				console.log('获取到了', arr)
				this.$http("post", "/BO/Industry_Company/AddIndustryCompany", [arr])
					.then((res) => {
						uni.hideLoading()
						console.log(res);
						if (res.Success) {
							this.$api.msg('提交成功')

						} else {
							this.$api.msg(res.Msg)
						}
					});
			}
		}
	}
</script>

<style scoped lang="scss">
	.company {
		padding: 20rpx;
		box-sizing: border-box;
		background-color: #fff;
		height: 100%;
	}
</style>